import goodsActions from 'actions/goods'
import commonActions from 'actions/common'
import { saveData } from 'utils'
import { message } from 'antd'

export default {
  state: {
    authorOptionsList: [],
    categoryOptions: []
  },
  reducers: {
    saveParams (state, payload) {
      state.searchParams = payload

      return state
    },
    save (state, payload) {
      return saveData(state, payload)
    }
  },
  effects: dispatch => ({
    // 获取回收站列表
    async getGoodsRecycleList (params) {
      this.save({
        recycleData: null
      })
      const res = await goodsActions.getGoodsRecycleList(params)
      if (res.success) {
        this.save({
          recycleData: {
            recycleList: res.data.recycleList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取作者列表
    async getAuthorList (params = {}) {
      this.save({
        authorOptionsList: []
      })
      const res = await goodsActions.getAuthorList(params)
      let author = res.data

      if (author) {
        author.map(item => {
          item.text = item.authorName
          item.value = item.authorId
        })
      }
      if (res.success) {
        this.save({
          authorOptionsList: author
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取类目列表
    async getCategoryOptions (params = {}, state, cb) {
      const res = await goodsActions.getCategoryOptions(params)
      let category = res.data

      if (category) {
        category.map(item => {
          item.text = item.name
          item.value = item.code
          item.title = item.name
        })
      }
      if (res.success) {
        if (params.parentId) {
          category.forEach(element => {
            element.isLeaf = true
            element.pId = params.parentId
          })
          this.save({
            categoryChildOptions: category
          })
          if (cb) {
            cb(category)
          }
        } else {
          this.save({
            categoryOptions: category
          })
        }
      } else {
        message.error(res.msg)
      }
    },
    // 新增创作者
    async addAuthor (params = {}) {
      this.save({
        addAuthorRequest: false
      })
      const res = await goodsActions.addAuthor(params)
      if (res.success) {
        this.save({
          addAuthorRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取创作者列表
    // 获取分类列表
    async getCategoryList (params) {
      this.save({
        categoryData: null
      })
      const res = await goodsActions.getCategoryList(params)

      if (res.success) {
        this.save({
          categoryData: {
            list: res.data.categoryResList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 还原商品
    async restoreGoods (params) {
      this.save({
        restoreGoodsRequest: false
      })
      const res = await goodsActions.restoreGoods(params)
      if (res.success) {
        this.save({
          restoreGoodsRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取商品列表
    async getGoodsList (params) {
      this.save({
        goodsData: null
      })
      this.saveParams(params)
      const res = await goodsActions.getGoodsList(params)

      if (res.success) {
        this.save({
          goodsData: {
            list: res.data.commodityInfoResList,
            total: res.data.totalCount,
            countInfo: res.data.countInfo
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    async getGoodsListCp (params, state) {
      this.save({
        goodsData: null
      })
      const res = await goodsActions.getGoodsList(state.goods.searchParams)

      if (res.success) {
        this.save({
          goodsData: {
            list: res.data.commodityInfoResList,
            total: res.data.totalCount,
            countInfo: res.data.countInfo
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 删除商品
    async delGoods (params) {
      this.save({
        delRequest: false
      })
      const res = await goodsActions.delGoods(params)

      if (res.success) {
        this.save({
          delRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 审核
    async auditGoods (params) {
      this.save({
        auditRequest: null
      })
      const res = await goodsActions.auditGoods(params)

      if (res.success) {
        this.save({
          auditRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 场景审核状态设置
    async handleAuditEdit (params, state) {
      const objCopy = state.goods.goodsData
      this.save({
        auditEditRequest: false
      })
      this.save({
        goodsData: null
      })
      setTimeout(() => {
        objCopy.list[params.index].auditStatus = params.auditStatus ? 'PASS' : 'REFUSE'
        this.save({
          auditEditRequest: true,
          goodsData: objCopy
        })
      }, 100)
    },
    // 获取详情
    async getGoodsDetails (params) {
      this.save({
        goodsDetails: null
      })
      const res = await goodsActions.getGoodsDetails(params)

      if (res.success) {
        this.save({
          goodsDetails: res.data
        })
      } else {
        message.error(res.msg)
      }
    },
    // 上架
    async putAway (params) {
      this.save({
        putAwayRequest: null
      })
      const res = await goodsActions.putAway(params)
      if (res.success) {
        this.save({
          putAwayRequest: true
        })
      } else {
        message.error(res.msg)
        this.getGoodsListCp()
      }
    },
    // 推荐
    async recommendGoods (params) {
      this.save({
        recommendGoodsRequest: null
      })
      const res = await goodsActions.recommendGoods(params)
      if (res.success) {
        this.save({
          recommendGoodsRequest: true
        })
      } else {
        message.error(res.msg)
        this.getGoodsListCp()
      }
    },
    // 上新
    async goodsUpdate (params) {
      this.save({
        goodsUpdateRequest: null
      })
      const res = await goodsActions.goodsUpdate(params)
      if (res.success) {
        this.save({
          goodsUpdateRequest: true
        })
      } else {
        message.error(res.msg)
        this.getGoodsListCp()
      }
    },
    // 新增商品
    async addGoods (params) {
      this.save({
        addRequest: null
      })
      const res = await goodsActions.addGoods(params)

      if (res.success) {
        this.save({
          addRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取配置
    async getConfig (params) {
      const res = await commonActions.getConfig(params)
      if (res.success) {
        this.save({
          unitList: res.data.dictResList[0].subDicts
        })
      }
    },
    // 获取库存列表
    async getStoreList (params) {
      this.save({
        storeData: null
      })
      const res = await goodsActions.getStoreList(params)

      if (res.success) {
        this.save({
          storeData: {
            list: res.data.outBoundList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取出库列表
    async getStoreOutList (params) {
      this.save({
        storeOutData: null
      })
      const res = await goodsActions.getStoreOutList(params)

      if (res.success) {
        this.save({
          storeOutData: {
            list: res.data.outBoundList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取用户
    async getAuthor (params) {
      this.save({
        author: null
      })
      const res = await goodsActions.getAuthor(params)

      if (res.success) {
        this.save({
          author: {
            list: res.data.authorResList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    async getAuthorRefresh (params) {
      const res = await goodsActions.getAuthor(params)

      if (res.success) {
        this.save({
          author: {
            list: res.data.authorResList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 删除创作者
    async delCreater (params) {
      this.save({
        delCreaterRequest: false
      })
      const res = await goodsActions.delCreater(params)

      if (res.success) {
        this.save({
          delCreaterRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 编辑作者
    async editCreater (params) {
      this.save({
        editCreaterRequest: false
      })
      const res = await goodsActions.editCreater(params)

      if (res.success) {
        this.save({
          editCreaterRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    async isShowCreater (params) {
      this.save({
        isShowRequest: false
      })
      const res = await goodsActions.isShowCreater(params)

      if (res.success) {
        this.save({
          isShowRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 编辑详情
    async editGoods (params) {
      this.save({
        editRequest: false
      })
      const res = await goodsActions.editGoods(params)

      if (res.success) {
        this.save({
          editRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    restGoods () {
      this.save({
        goodsData: null
      })
    },
    // 新增库存
    async addStore (params) {
      this.save({
        addStoreRequest: false
      })
      const res = await goodsActions.addStore(params)

      if (res.success) {
        this.save({
          addStoreRequest: true
        })
      } else {
        message.error(res.msg)
      }
    }
  })
}
